package com.example.intermediateactivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.example.intermediateactivity.adapters.CustomBaseAdapter;
import com.example.intermediateactivity.beans.Dog;
import com.example.intermediateactivity.utils.ToastHelper;

import java.util.List;

public class BaseAdapterActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private List<Dog> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);

        // 获取默认list数据，用于列表元素
        list = Dog.getDefaultList();
        // 获取列表容器activity
        Spinner spBase = findViewById(R.id.sp_base);
        // 创建BaseAdapter
        BaseAdapter adapter = new CustomBaseAdapter(this, list);
        // 列表设置适配器
        spBase.setAdapter(adapter);
        // 设置默认选中第一项
        spBase.setSelection(0);
        // 添加监听事件
        spBase.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        ToastHelper.show(this, "选中了" + list.get(i).name);
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}